package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.word.DataRegionInsertType;
import com.zhuozhengsoft.pageoffice.word.DataRegionWriter;
import com.zhuozhengsoft.pageoffice.word.WordDocumentWriter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;


@RestController
@RequestMapping(value = "/InsertPageBreak2")
public class InsertPageBreak2Controller {
    private final String dir = ResourceUtils.getURL("classpath:").getPath() + "static/doc/";

    public InsertPageBreak2Controller() throws FileNotFoundException {
    }

    @RequestMapping(value = "/Word")
    public String showWord(HttpServletRequest request) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);

        WordDocumentWriter doc = new WordDocumentWriter();
        DataRegionWriter mydr1 = doc.createDataRegion("PO_first", DataRegionInsertType.After, "[end]");
        mydr1.selectEnd();
        doc.insertPageBreak();//插入分页符
        DataRegionWriter mydr2 = doc.createDataRegion("PO_second", DataRegionInsertType.After, "[end]");
        mydr2.setValue("[word]/doc/InsertPageBreak2/test2.docx[/word]");
        poCtrl.setWriter(doc);

        //打开文档
        poCtrl.webOpen("/doc/InsertPageBreak2/test1.docx", OpenModeType.docNormalEdit, "张三");
        return poCtrl.getHtml();
    }


    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "InsertPageBreak2/" + fs.getFileName());
        fs.close();
    }

}
